#include <algorithm>
#include <vector>
#include <cmath>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <set>
#include <map>
#include <cstdlib>
#define LL long long
#define ldb long double
const ldb eps = 1e-9;
const ldb pi = fabsl(atan2(0.0, -1.0));
const int INF = 0x7f7f7f7f;
const LL LINF = 1ll << 60;
const ldb LDINF = 1e42;
#define nextLine() {int c = 0; while ((c = getchar()) != 10 && c != EOF);}
#define fill(a, c) memset(a, c, sizeof a)
#define debug(a) cerr << #a << " " << a << " ";
#define debugl(a) cerr << #a << " " << a << "\n";
#define addEdge(a, b) next[edges] = first[a]; first[a] = edges; end[edges] = b; edges++;
using namespace std;

LL l, r;
int t;
const int M = 504;
int globmark;
LL dp[M][48][21][2][3];
int pre[M][48][2];
class My
{
	public:
		int a, b, c, d, e;
		My(){}
		My(int a, int b, int c, int d, int e) : a(a), b(b), c (c), d(d), e(e){}
};

vector <My> used;
//bool was[M][48][21][2][3];
int g[48][10];
int a[20];
int n;


void Load()
{
    cin >> l >> r;
}

int check(int mask, int m, int m5)
{
	int &res = pre[m][mask][m5];
	if (res != -1) return res;
    res = 1;

    if (m % (1 << (mask % 4)) != 0) res = 0;
    mask /= 4;
    if (m % 3 != 0 && (mask % 3) == 1) res = 0;
    if (m % 9 != 0 && (mask % 3) == 2) res = 0;
    mask /= 3;
    if (m5 == 0 && (mask % 2) == 1) res = 0;
    mask /= 2;
    if (m % 7 != 0 && (mask % 2) == 1) res = 0;
    return res;
}

int getNum(int mask)
{
	int num = 1 << (mask % 4);
    mask /= 4;
   	if (mask % 3 == 1)
   		num *= 3;
   	else if (mask % 3 == 2)
    	num *= 9;
    mask /= 3;
    if (mask % 2 == 1)
    	num *= 5;
    mask /= 2;
    if (mask % 2 == 1)
    	num *= 7;
    return num;
}

int getMask(int x)
{
	int nmask = 0;
	while (x % 7 == 0)
	{
		nmask += 1;
		x /= 7;
	}
	nmask *= 2;
	while (x % 5 == 0)
	{
		nmask += 1;
		x /= 5;
	}
	nmask *= 3;
	while (x % 3 == 0)
	{
		nmask += 1;
		x /= 3;	
	}
	nmask *= 4;
	while (x % 2 == 0)
	{
		nmask += 1;
		x /= 2;
	}
	return nmask;
}

LL f(int i, int mask, int m, int m5, int less)
{
	LL &res = dp[m][mask][i][m5][less];
    if (res != -1) return res;
    if (less == 0)
    {
    	used.push_back(My(m, mask, i, m5, less));
    }
    if (i == 0)
    {
        if (less == 2)
        {
        	res = 0;
        }
        else
        {
        	//debug(mask); debug(m); debug(m5);  cerr << check(mask, m, m5) << "\n";
            if (check(mask, m, m5)) res = 1;  
            else res = 0;
        }
        return res;
    }
    res = 0;
    if (check(mask, m, m5))
        res++;
    int nless, nm, nm5;
    int j;
    for (j = 0; j <= 9; j++)
    {
        nless = less;
        if (less == 0 && j < a[i - 1]) nless = 1;
        if (less == 0 && j > a[i - 1]) nless = 2;
        nm = (m * 10 + j) % M;
        nm5 = (j == 0) || (j == 5);
        res += f(i - 1, g[mask][j], nm, nm5, nless);
    }
    return res;
}

LL count(LL N)
{
    if (N == 0) return 0;
    n = 0;
    while (N > 0)
    {
        a[n++] = N % 10;
        N /= 10;
    }
    LL res = 0;
    int nless;
    int i;
    for (i = 0; i < used.size(); i++)
    {
    	dp[used[i].a][used[i].b][used[i].c][used[i].d][used[i].e] = -1;
    }
    used.resize(0);
    for (i = 1; i <= 9; i++)
    {
        if (i < a[n - 1]) nless = 1;
        else if (i == a[n - 1]) nless = 0;
        else nless = 2;
        res += f(n - 1, g[0][i], i % M, i == 5, nless);
    }
    return res;
}

void Solve()
{
	LL a = count(r);
	LL b = count(l - 1);
    cout << a - b << "\n";
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
    #endif
    cin >> t;
    int i;
    fill(pre, -1);
    fill(dp, -1);
    int j;
    int num;
    for (i = 0; i < 2 * 2 * 3 * 4; i++)
    {
    	num = getNum(i);
    	g[i][0] = g[i][1] = i;
    	for (j = 2; j <= 9; j++)
			g[i][j] = getMask(num * j / __gcd(num, j));
    }         
    for (i = 0; i < t; i++)
    {
        Load();
        Solve();
    }
    return 0;
}
